//****************************************************
// Porman Project 2
// Filename: CDepthStencilObject.cpp
// Date: 2009/05/31
// Author: KengYu Lin
// Modify:
//****************************************************
#include "CRenderDevice.h"
#include "CDepthStencilObject.h"
#include "CAssert.h"

namespace Porman
{

namespace GL
{

CDepthStencilObject::CDepthStencilObject( CRenderDevice* pDevice ) :
CBaseObject(pDevice)
{
    glGenRenderbuffers( 1, &m_uiDepthStencilObjID );
    if( m_uiDepthStencilObjID != 0 )
    {
        setName("Non");
        m_iNumOfDepthStencilObj++;
    }
    else
        throw 0;
}

CDepthStencilObject::~CDepthStencilObject( void )
{
    if( m_uiDepthStencilObjID != 0)
    {
        glDeleteRenderbuffers(1, &m_uiDepthStencilObjID);
        m_uiDepthStencilObjID = 0;
    }
    m_iNumOfDepthStencilObj--;
}

ERESULT
CDepthStencilObject::setDepthStencil( int iWidth, int iHeight )
{
    THREAD_CHECKING(m_pDeviceRef->getCurThreadID(), E_THREAD_ERROR);

    ERESULT enumResult = E_SUCCESS;
    glBindRenderbuffer( GL_RENDERBUFFER_EXT, m_uiDepthStencilObjID );

    glRenderbufferStorage( GL_RENDERBUFFER_EXT, GL_DEPTH_STENCIL_EXT, (unsigned int)iWidth, (unsigned int)iHeight );
    switch( glGetError() )
    {
    case GL_OUT_OF_MEMORY:
        enumResult = E_OUT_OF_MEMORY;
    case GL_INVALID_VALUE:
        enumResult = E_INVALID_VALUE;
    }

    glBindRenderbuffer( GL_RENDERBUFFER_EXT, 0 );

    return enumResult;
}

}//End namespace GL

}//End namespace Porman